import datetime

def Load_Segments(database, corpus, limits=0, sort=False):
    segmentsList = []
    #
    if corpus == "":
        filter = {}
        if limits != 0:
            filter["limit"] = limits
        articleDocs = database.Find("Recommender", "Article",filter=filter)
        #
        for articleDoc in articleDocs:
            segmentsList.append(articleDoc["segments"])
        #
        return segmentsList
    #
    idList = database.Find("Recommender", 'Corpus', filter={"Name":corpus})[0]['IDList']

    #
    articleDocs = database.Find("Recommender", "Article")

    if not sort:
        for articleDoc in articleDocs:
           if articleDoc["id"] in idList:
               segmentsList.append(articleDoc["segments"])
               if limits != 0 and len(segmentsList) >= limits:
                   break

    #
    else:
        articleByIndex = {}
        for articleDoc in articleDocs:
            articleByIndex[articleDoc["id"]] = articleDoc

        #
        for id in idList:
            articleDoc = articleByIndex[id]
            segmentsList.append(articleDoc["segments"])
            if limits != 0 and len(segmentsList) >= limits:
                break

    #
    return segmentsList


def Batch_Loop(database, databaseName, tableName, callback, params={}, batch_size=1000000, printField="", filter={}):
    # 计算循环批次数 batch count

    #total_Count = database.Count(databaseName, tableName, filter)
    total_Count = database.Join_Count("rec_article", "article_action","article_tag","article_id", "article_id", filter = filter)

    batch_count = int(total_Count / batch_size)
    if total_Count % batch_size != 0:
        batch_count += 1

    # 分批次循环
    count = 0
    for i in range(batch_count):
        start = i * batch_size
        end = start + batch_size

        filter["skip"] = start
        filter["limit"] = batch_size
        documents = database.Join_Find("rec_article", "article_action","article_tag","article_id", "article_id", filter = filter)

        #
        if printField != "":
            length = len(documents)
            print("")
            print("batch_count", i, datetime.datetime.now())
            print("Start", documents[0][printField])
            print("End", documents[length - 1][printField])

        #
        count = callback(count, documents, params)
        documents = None

    # ---End of Loop---
    return count


def WordWeightListToDict(list):
    weightByWord = {}
    for pair in list:
        weightByWord[pair["Word"]] = pair["Weight"]
    return weightByWord

